<?php
class CatalogAction extends BaseAction {
	
	const TYPE = 'catalog';
	
	public function index() {
		
		$catalogList = CatalogHelper::getList("pid=0","order_num DESC");
		$subCatalogList = CatalogHelper::getList("pid<>0","order_num DESC");
		
		$this->assign("catalogList", $catalogList);
		$this->assign("subCatalogList", $subCatalogList);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	//[操作流]默认页面{修改} Or {新增}
	//当传过来的ID有值，则进行修改操作流
	//当传过来的ID没值，则进行新增操作流
	public function todo(){
		$referUrl = $_SERVER['HTTP_REFERER'];
		$id = $this->_get("id");
		$obj = CatalogHelper::getObject("catalog_id={$id}");
		$catalogList = CatalogHelper::getList("pid=0","order_num DESC");
		$subCatalogList = CatalogHelper::getList("pid<>0","order_num DESC");
		$dynamicModuleList = DynamicModuleHelper::getList();
		$reserveModuleList = ReserveModuleHelper::getList();
		$templateList = CommonUtil::getFileList(CommonUtil::getTemplatePath());
		
		$this->assign("obj",$obj);
		$this->assign("catalogList", $catalogList);
		$this->assign("subCatalogList", $subCatalogList);
		$this->assign("dynamicModuleList",$dynamicModuleList);
		$this->assign("reserveModuleList",$reserveModuleList);
		$this->assign("templateList",$templateList);
		$this->assign("referUrl",$referUrl);
		$this->assign("type", self::TYPE);
		$this->display();
	}
	
	public function add(){
		
		$is_external_link = $this->_post("is_external_link");	//栏目类型是否是｛系统外链｝
		$data['catalog_name'] = $is_external_link == 0?$this->_post("catalog_name") : $this->_post("catalog_name2");
		$data['catalog_eng_name'] = $is_external_link == 0?$this->_post("catalog_eng_name") : $this->_post("catalog_eng_name2");
		$data['pid'] = $is_external_link == 0?$this->_post("pid") : $this->_post("pid2");
		$tempImgAddr = $is_external_link == 0?$this->_post("picture_text") : $this->_post("picture2_text");	//图片临时地址
		$data['is_hidden'] =$is_external_link == 0?$this->_post("is_hidden") : $this->_post("is_hidden2");
		if($data['is_hidden'] != 1){
			$data['is_hidden'] = 0;
		}
		
		//验证规则.....
		if(empty($data['catalog_name'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "名称不能为空！");
			return;
		}
		if($data['pid'] < 0){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择上级分类！");
			return;
		}
		if($data['pid'] != 0){
			$obj = CatalogHelper::getObject("catalog_id=".$data['pid']);	//更改选择的｛父ID｝对象
			if(empty($obj)){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "你选择的上级分类不存在！");
				return;
			}
			if($obj["pid"] == 0){	//父类为根目录
				$data["root_id"] = $obj["pid"];
			}else{
				$data["root_id"] = $obj["root_id"];
			}
		}else {
			$data["root_id"] = 0;
		}
		//URL跳转模块.....[验证规则].........
		if($is_external_link == 1){
			$data['catalog_type'] = CatalogType::$REDIRECT;
			$data['redirect_url'] = $this->_post('redirect_url');
			if(!ValidateUtil::isURL($data['redirect_url'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请输入正确的跳转网址！");
				return;
			}
		}
		if($is_external_link == 0){	//系统内链验证规则
			$data['catalog_type'] = $this->_post('catalog_type');
			$data['catalog_url_name'] =$this->_post('catalog_url_name');
			$data['seo_title'] =$this->_post('seo_title');
			$data['seo_keywords'] =$this->_post('seo_keywords');
			$data['seo_description'] =$this->_post('seo_description');
			
			if(empty($data['catalog_type'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择类型！");
				return;
			}
			//只有[动态模块]才有列表页分页
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$page_show_num =$this->_post('page_show_num');
				$data['page_show_num'] = CommonUtil::parseInt($page_show_num);
				if($data['page_show_num'] <= 0){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请输入大于0的数字！");
					return;
				}
			}
			//只有[动态模块]、[表单模块]才有dynamic_module_id
			if(!CommonUtil::contain($data['catalog_type'], "dynamic:") && $data['catalog_type'] != CatalogType::$RESERVE_MODULE){
				$data['dynamic_module_id'] = 0;
			}
			//验证“URL路径”唯一性
			if(empty($data['catalog_url_name'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请填写URL路径！");
				return;
			}
			$obj4check = CatalogHelper::getObject("catalog_url_name='{$data['catalog_url_name']}'");
			if(!empty($obj4check)){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "URL路径处重复，请重新填写！");
				return;
			}
			//动态模块含有[列表模版]
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$data['list_module_filepath'] = $this->_post('list_module_filepath');
				if(empty($data['list_module_filepath'])){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择列表模版！");
					return;
				}
			}
			//所有模块都含有[内容模版]
			$data['view_module_filepath'] = $this->_post('view_module_filepath');
			if(empty($data['view_module_filepath'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请选内容模版！");
				return;
			}
			//动态模型模块.....[验证规则].........
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$data['dynamic_module_id'] = str_replace("dynamic:","",$data['catalog_type']);
				$data['catalog_type'] = CatalogType::$DYNAMIC_MODULE;
				$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$data['dynamic_module_id']}");
				if(empty($dynamicModule)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择正确的类型！");
					return;
				}
			}
			//表单模型模块.....[验证规则].........
			if($data['catalog_type'] == CatalogType::$RESERVE_MODULE){
				$data['dynamic_module_id'] = $this->_post("reserve_module_id");
				$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$data['dynamic_module_id']}");
				if(empty($reserveModule)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择正确的表单模型！");
					return;
				}
			}
		}
		//------------------------临时文件转移 开始--------------------------------------------------
		if(!empty($tempImgAddr)){
			$imgAddr = CommonUtil::getRemovedProductPath($tempImgAddr);
			$data['picture_addr'] = $imgAddr;
		}
		//------------------------临时文件转移结束--------------------------------------------------
		
		
		$result = CatalogHelper::add($data);
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		}else{	//添加成功
			//只有数据库更新成功，才将文件进行真正的操作
			if(!empty($tempImgAddr)){
				CommonUtil::remove2ProductPath($tempImgAddr);
			}
			echo CommonUtil::ajaxReturn(Status::$REDIRECT,null, __APP__.'/center/catalog/');
		}
	}
	
	public function update(){
		$id = $this->_post("id");
		$is_external_link = $this->_post("is_external_link");	//栏目类型是否是｛系统外链｝
		$data['catalog_name'] = $is_external_link == 0?$this->_post("catalog_name") : $this->_post("catalog_name2");
		$data['catalog_eng_name'] = $is_external_link == 0?$this->_post("catalog_eng_name") : $this->_post("catalog_eng_name2");
		$data['pid'] = $is_external_link == 0?$this->_post("pid") : $this->_post("pid2");
		$tempImgAddr = $is_external_link == 0?$this->_post("picture_text") : $this->_post("picture2_text");	//图片临时地址
		$imgAddr = CommonUtil::getRemovedProductPath($tempImgAddr);	//获取到图片正式环境下的地址
		$data['is_hidden'] =$is_external_link == 0?$this->_post("is_hidden") : $this->_post("is_hidden2");
		if($data['is_hidden'] != 1){
			$data['is_hidden'] = 0;
		}
		
		//验证规则.....
		$catalog = CatalogHelper::getObject("catalog_id={$id}");
		if(empty($catalog)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "你要操作的对象不存在！");
			return;
		}
		if(empty($data['catalog_name'])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "名称不能为空！");
			return;
		}
		if($data['pid'] < 0){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择上级分类！");
			return;
		}
		if($data['pid'] != 0){
			$obj = CatalogHelper::getObject("catalog_id=".$data['pid']);	//更改选择的｛父ID｝对象
			if(empty($obj)){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "你选择的上级分类不存在！");
				return;
			}
			if($obj["pid"] == 0){	//父类为根目录
				$data["root_id"] = $obj["pid"];
			}else{
				$data["root_id"] = $obj["root_id"];
			}
		}else {
			$data["root_id"] = 0;
		}
		//URL跳转模块.....[验证规则].........
		if($is_external_link == 1){
			$data['catalog_type'] = CatalogType::$REDIRECT;
			$data['redirect_url'] = $this->_post('redirect_url');
			$data['catalog_url_name'] = "";
			$data['dynamic_module_id'] = 0;
			if(!ValidateUtil::isURL($data['redirect_url'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请输入正确的跳转网址！");
				return;
			}
		}
		if($is_external_link == 0){	//系统内链验证规则
			$data['catalog_type'] = $this->_post('catalog_type');
			$data['catalog_url_name'] =$this->_post('catalog_url_name');
			$data['seo_title'] =$this->_post('seo_title');
			$data['seo_keywords'] =$this->_post('seo_keywords');
			$data['seo_description'] =$this->_post('seo_description');
			
			if(empty($data['catalog_type'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择类型！");
				return;
			}
			//只有[动态模块]才有列表页分页
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$page_show_num =$this->_post('page_show_num');
				$data['page_show_num'] = CommonUtil::parseInt($page_show_num);
				if($data['page_show_num'] <= 0){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请输入大于0的数字！");
					return;
				}
			}
			//只有[动态模块]、[表单模块]才有dynamic_module_id
			if(!CommonUtil::contain($data['catalog_type'], "dynamic:") && $data['catalog_type'] != CatalogType::$RESERVE_MODULE){
				$data['dynamic_module_id'] = 0;
			}
			//验证“URL路径”唯一性
			if(empty($data['catalog_url_name'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请填写URL路径！");
				return;
			}
			if($data['catalog_url_name'] != $catalog['catalog_url_name']){
				$obj4check = CatalogHelper::getObject("catalog_url_name='{$data['catalog_url_name']}'");
				if(!empty($obj4check)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "URL路径处重复，请重新填写！");
					return;
				}
			}
			//动态模块含有[列表模版]
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$data['list_module_filepath'] = $this->_post('list_module_filepath');
				if(empty($data['list_module_filepath'])){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择列表模版！");
					return;
				}
			}
			//所有模块都含有[内容模版]
			$data['view_module_filepath'] = $this->_post('view_module_filepath');
			if(empty($data['view_module_filepath'])){
				echo CommonUtil::ajaxReturn(Status::$ERROR, "请选内容模版！");
				return;
			}
			//动态模型模块.....[验证规则].........
			if(CommonUtil::contain($data['catalog_type'], "dynamic:")){
				$data['dynamic_module_id'] = str_replace("dynamic:","",$data['catalog_type']);
				$data['catalog_type'] = CatalogType::$DYNAMIC_MODULE;
				$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$data['dynamic_module_id']}");
				if(empty($dynamicModule)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择正确的类型！");
					return;
				}
			}
			//表单模型模块.....[验证规则].........
			if($data['catalog_type'] == CatalogType::$RESERVE_MODULE){
				$data['dynamic_module_id'] = $this->_post("reserve_module_id");
				$reserveModule = ReserveModuleHelper::getObject("reserve_module_id={$data['dynamic_module_id']}");
				if(empty($reserveModule)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "请选择正确的表单模型！");
					return;
				}
			}
		}
		
		//------------------------临时文件转移 开始--------------------------------------------------
		if($catalog["picture_addr"] != $imgAddr){
			$data['picture_addr'] = $imgAddr;
		}
		//------------------------临时文件转移结束--------------------------------------------------
		
		//更新节点以及子节点的PID和ROOTID
		$result = CatalogHelper::refreshCataWithTrans($data,$id);
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		}else{	//修改成功，提示成功信息，并且返回上一个页面
			//只有数据库更新成功，才将文件进行真正的操作
			if($catalog["picture_addr"] != $imgAddr){
				//删除前一任的文件，再进行转移临时文件
				if(!empty($catalog["picture_addr"])){
					CommonUtil::deleteFile($catalog["picture_addr"]);
				}
				CommonUtil::remove2ProductPath($tempImgAddr);
			}
			echo CommonUtil::ajaxReturn(Status::$REDIRECT, "修改成功！",__APP__.'/center/catalog/');
		}
		
	}
	
	public function delete() {
		
		$id = $this->_get("id");
		
		//该ID是否存在
		$obj = CatalogHelper::getObject("catalog_id={$id}");
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "对象不存在，操作失败！");
			return ;
		}
		//是否有 子类...
		$list = CatalogHelper::getList("pid=".$id);
		if(!empty($list)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "请先删除子分类后，再操作！");
			return ;
		}
		//是否有 数据...
		if($obj["catalog_type"] == CatalogType::$DYNAMIC_MODULE){
			$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$obj["dynamic_module_id"]}");
			if(!empty($dynamicModule)){
				$sql = "SELECT * FROM ".C("DB_PREFIX").$dynamicModule["table_name"]." WHERE catalog_id={$obj["catalog_id"]}";
				$list = DynamicModuleHelper::queryList($sql);
				if(!empty($list)){
					echo CommonUtil::ajaxReturn(Status::$ERROR, "该栏目下有数据，请先删除或转移数据后再操作！");
					return ;
				}
			}
		}
		//删除“已上传文件”
		if(!empty($obj['picture_addr'])){
			CommonUtil::deleteFile($obj['picture_addr']);
		}
		//删除是否成功
		$result = CatalogHelper::delete("catalog_id={$id}");
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "系统繁忙，请稍后重试！");
			return ;
		}
		
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
		
	}
	
	//修改推荐值  操作
    public function change_order_num(){
        $id = $this->_post("id");
        $orderNum = $this->_post('order_num');
        $data["order_num"] = $orderNum;
        $result = CatalogHelper::update($data,"catalog_id={$id}");
        if($result === false){
            echo CommonUtil::ajaxReturn(Status::$ERROR,"服务器繁忙，请稍后重试！");
        }else{
            echo CommonUtil::ajaxReturn(Status::$SUCCESS,"修改成功！");
        }
    }
	
	
}